package com.kingdee.hackson.service.impl;

import com.kingdee.hackson.service.IInitOrgIdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class InitOrgIdServiceImpl implements IInitOrgIdService {
    private static final Logger LOGGER = LoggerFactory.getLogger(InitOrgIdServiceImpl.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private final String longCodeFiled = "fadminstruct";//fadminstruct
    // 包含下级的测试组织ID
    private final Long parentId = 1269600871000000103L;

    // 5.6w下级组织 占用1.4M内存(对象头16字节+值8字节)
    public List<Long> getHasrightOrgId() {
        long start1 = System.currentTimeMillis();
        List<String> org1 = jdbcTemplate.query("select " + longCodeFiled + " from t_haos_adminorg where fid=" + parentId, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getString(1);
            }
        });
        LOGGER.info("查询上级组织长编码耗时：" + (System.currentTimeMillis() - start1));
        long start2 = System.currentTimeMillis();
        List<Long> org2 = jdbcTemplate.query("select fid from t_haos_adminorg where " + longCodeFiled + " like '" + org1.get(0) + "%'", new RowMapper<Long>() {
            @Override
            public Long mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getLong(1);
            }
        });
        LOGGER.info("查询所有下级组织ID耗时：" + (System.currentTimeMillis() - start2)+" count="+org2.size());
        return org2;
    }
}
